#include "stdafx.h"
#include "util.h"
#include "DlgDirectAccess.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CDlgDirectAccess 


CDlgDirectAccess::CDlgDirectAccess(CFGCamera *pCamera,CWnd* pParent)
	: CDialog(CDlgDirectAccess::IDD, pParent)
{
  m_pCamera=pCamera;
  m_Cnt=0;
	//{{AFX_DATA_INIT(CDlgDirectAccess)
	//}}AFX_DATA_INIT
}


void CDlgDirectAccess::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgDirectAccess)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgDirectAccess, CDialog)
	//{{AFX_MSG_MAP(CDlgDirectAccess)
	ON_BN_CLICKED(IDB_READ, OnRead)
	ON_BN_CLICKED(IDB_WRITE, OnWrite)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Assign camera object.

BOOL CDlgDirectAccess::OnInitDialog() 
{
  CDialog::OnInitDialog();
	
  GetDlgItem(IDE_ADDRESS)->SetWindowText("F0F00614");
  GetDlgItem(IDE_DATA)->SetWindowText("00000000");

  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// Read quadlet from camera.

void CDlgDirectAccess::OnRead() 
{
  char   cbuf[32];
  UINT8  Buf[8];
  UINT32 Address,Result,Quadlet;
  CListBox *pList;
  int    Ix;

  GetDlgItem(IDE_ADDRESS)->GetWindowText(cbuf,sizeof(cbuf));
  if(strlen(cbuf)!=8)
   return;

  if(ScanHex(cbuf,Buf,sizeof(Buf))!=4)
   return;
  
  Address=MAKEULONG(Buf[0],Buf[1],Buf[2],Buf[3]); 

  Result=m_pCamera->ReadRegister(Address,&Quadlet);
  if(Result==FCE_NOERROR)
   wsprintf(cbuf,"%d: %08X -> %08X",++m_Cnt,Address,Quadlet);
  else
   wsprintf(cbuf,"%d: Error %08X",++m_Cnt,Result);

  pList=(CListBox*)GetDlgItem(IDL_HISTORY);
  Ix=pList->AddString(cbuf);
  pList->SetCurSel(Ix);
}

/////////////////////////////////////////////////////////////////////////////
// Write quadlet to camera.

void CDlgDirectAccess::OnWrite() 
{
  char   cbuf[32];
  UINT8  Buf[8];
  UINT32 Address,Data,Result;
  CListBox *pList;
  int    Ix;

  GetDlgItem(IDE_ADDRESS)->GetWindowText(cbuf,sizeof(cbuf));
  if(strlen(cbuf)!=8)
   return;
  if(ScanHex(cbuf,Buf,sizeof(Buf))!=4)
   return;
  Address=MAKEULONG(Buf[0],Buf[1],Buf[2],Buf[3]); 

  GetDlgItem(IDE_DATA)->GetWindowText(cbuf,sizeof(cbuf));
  if(strlen(cbuf)!=8)
   return;
  if(ScanHex(cbuf,Buf,sizeof(Buf))!=4)
   return;
  Data=MAKEULONG(Buf[0],Buf[1],Buf[2],Buf[3]); 

  Result=m_pCamera->WriteRegister(Address,Data);
  if(Result==FCE_NOERROR)
   wsprintf(cbuf,"%d: %08X <- %08X",++m_Cnt,Address,Data);
  else
   wsprintf(cbuf,"%d: Error %08X",++m_Cnt,Result);

  pList=(CListBox*)GetDlgItem(IDL_HISTORY);
  Ix=pList->AddString(cbuf);
  pList->SetCurSel(Ix);
}
